﻿

var sendReq = getXmlHttpRequestObject();
var receiveReq = getXmlHttpRequestObject();
var lastMessage = 0;
var mTimer;

function HelloWorld()
{
    myObj = new person("Andrew","Yin");
    myObj["firstName"] = "Jin Yu";
    myObj["lastName"] = "Yin";
    //TestWebService.HelloWorld(myObj,SucceededCallback);
}//fm

function getXmlHttpRequestObject() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        document.getElementById('p_status').innerHTML =
		'Status: Cound not create XmlHttpRequest Object.' +
		'Consider upgrading your browser.';
    }
} //fm


//Gets the current messages from the server
function getChatText() {
    if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
        receiveReq.open("GET", 'Chat.aspx?chat=1&last=' + lastMessage, true);
        receiveReq.onreadystatechange = handleReceiveChat;
        receiveReq.send(null);
    }
} //fm


//Function for handling the return of chat text
function handleReceiveChat() {
	if (receiveReq.readyState == 4) {
		var chat_div = document.getElementById('div_chat');
		var xmldoc = receiveReq.responseXML;
		var message_nodes = xmldoc.getElementsByTagName("text"); 
		var n_messages = message_nodes.length
		for (i = 0; i < n_messages; i++) {
			var user_node = message_nodes[i].getElementsByTagName("user");
			var text_node = message_nodes[i].getElementsByTagName("text");
			var time_node = message_nodes[i].getElementsByTagName("time");
			chat_div.innerHTML += user_node[0].firstChild.nodeValue + '&nbsp;';
			chat_div.innerHTML += '<font class="chat_time">' 
			chat_div.innerHTML += time_node[0].firstChild.nodeValue + '</font><br />';
			chat_div.innerHTML += text_node[0].firstChild.nodeValue + '<br />';
			lastMessage = (message_nodes[i].getAttribute('id'));
		}
		mTimer = setTimeout('getChatText();',2000);
	}//fm
	
	//message type <<
//	<root>
//	<message id="10">
//		<user>Ryan Smith</user>
//		<text>Here is the Message Text</text>
//		<time>07:53</time>
//	</message>
//    </root>


//Add a message to the chat server.
function sendChatText() {
	if (sendReq.readyState == 4 || sendReq.readyState == 0) {
		sendReq.open("POST", 'getChat.php?chat=1&last=' + lastMessage, true);
		sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		sendReq.onreadystatechange = handleSendChat; 
		var param = 'message=' + document.getElementById('txt_message').value;
		param += '&name=Ryan Smith';
		param += '&chat=1';
		sendReq.send(param);
		document.getElementById('txt_message').value = '';
	}							
}//fm

//When our message has been sent, update our page.
function handleSendChat() {
	//Clear out the existing timer so we don't have 
	//multiple timer instances running.
	clearInterval(mTimer);
	getChatText();
}//fm

//This cleans out the database so we can start a new chat session.
function resetChat() {
	if (sendReq.readyState == 4 || sendReq.readyState == 0) {
		sendReq.open("POST", 'chat/getChat.php?chat=1&last=' + lastMessage, true);
		sendReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		sendReq.onreadystatechange = handleResetChat; 
		var param = 'action=reset';
		sendReq.send(param);
		document.getElementById('txt_message').value = '';
	}							
}//fm

//This function handles the response after the page has been refreshed.
function handleResetChat() {
	document.getElementById('div_chat').innerHTML = '';
	clearInterval(mTimer);
	getChatText();
}//fm



 

